package com.example.makerspace;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.makerspace.dto.robotDTO.Perception;
import com.example.makerspace.dto.robotDTO.RobotDTO;
import com.example.makerspace.dto.robotDTO.UserInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

//模拟浏览器发送http请求完成固定地址的请求
public class HttpClientUtils {



    public static void main(String[] args) throws IOException {
        String url = "https://debank.com/profile/0xc64994613897d24b2119e881a611e9702bb87ba8";
        String html = fetchHTML(url);
        System.out.println(html);

        //1.生成httpclient，相当于该打开一个浏览器
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//        CloseableHttpResponse response = null;
//        //2.创建get请求，相当于在浏览器地址栏输入 网址
//        HttpGet request = new HttpGet("https://debank.com/profile/0xc64994613897d24b2119e881a611e9702bb87ba8");
//        try {
//            //3.执行get请求，相当于在输入地址栏后敲回车键
//            response = httpClient.execute(request);
//
//            //4.判断响应状态为200，进行处理
//            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                //5.获取响应内容
//                HttpEntity httpEntity = response.getEntity();
//                String html = EntityUtils.toString(httpEntity, "utf-8");
//                System.out.println(html);
//            } else {
//                //如果返回状态不是200，比如404（页面不存在）等，根据情况做处理，这里略
//                System.out.println("返回状态不是200");
//                System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
//            }
//        } catch (ClientProtocolException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            //6.关闭
//        }
    }
    public static String fetchHTML(String urlStr) {
        StringBuilder content = new StringBuilder();
        try {
            URL url = new URL(urlStr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine())!= null) {
                content.append(inputLine);
            }
            in.close();
            con.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content.toString();
    }



}
